define(function(require, exports, module) {
	"use strict";
	require("mui");
	require("jquery");
	require("http://g.alicdn.com/ilw/ding/0.8.9/scripts/dingtalk.js");
	var abpdata = require("abpinit");
	var abp = abpdata.abpstart;
	var template = require("template");

	//调用日期筛选传值
	require("getSearchDateItem");
	//调用状态筛选值
	require("recordStatusID");
	var muiTips = require("muiTips"); //封装的弹框
	var keyWordText = ""; //关键字搜索

	var sourceType = abp.sourceType; //获取权限类型接口数据
	var authorityInit = require("operatAuthority"); //调用封装的权限方法
	var authorityData = sessionStorage.getItem('operatAuthorityData'); //获取权限数据
	var authorityIdList = authorityInit.getJurisdictionId(authorityData); //权限一级判断
	var authorityPurchase = authorityInit.getJurisdiction(authorityData, sourceType.BUY); //采购单权限判断
	//返回清除缓存
	var commonBack = require("commonBack");
	//钉钉返回
	var dingTalkNav = require('dd_config');

	function getRightFun() {
		document.location.href = "../../src/newBuy/newBuy.html?dd_nav_bgcolor=FFFF943E";
	}
	if(authorityIdList.indexOf(sourceType.BUY) === -1) { //一级权限判断
		$('.mui-content').addClass('initContentTips');
		$('.mui-content > .tipsConInit').html("您没有权限操作采购订单哦!");
		$("ul.pop-tab > li").addClass('mui-disabled');
		$("header > a").remove();
		dingTalkNav.RightNavBar(false, "新增", "default");
	} else {
		if(authorityPurchase.indexOf(2) === -1) { //判断是否有添加复制权限
			$("header > a").remove();
			dingTalkNav.RightNavBar(false, "新增", "default");
		} else {
			$("header > a").show();
			dingTalkNav.RightNavBar(true, "新增", getRightFun);
		}
		if(authorityPurchase.indexOf(1) === -1) { //判断是否有查询权限
			$('.mui-content > .tipsConInit').html("您没有权限查看采购记录哦!");
			$('.mui-content').addClass("initContentTips");
			$("ul.pop-tab > li").addClass('mui-disabled');
		} else {

			var getSearchItem = require("getSearchItem"); //调用封装搜索
			getSearchItem.getSearchItem("#chioce-search"); //添加搜索效果
			getSearchItem.noSearchTap("ul.pop-tab .pop-tab-titleitem"); //点击其他遮罩层关闭搜索

			$("body").on("keyup", "#searchText", function() { //搜索框事件
				keyWordText = $("#searchText").val();
				getClientList("pirchaseList", "#listPirchase", "searchContentTips", false);
			});

			var startDate = ''; //开始时间
			var endDate = ''; //结束时间
			var strStatusId = []; //id筛选
			strStatusId[0] = '';
			var pageIndex = 1; //当前分页
			var dataState = true; //判断如何渲染页面，true替换加载，false加载在末尾
			var pageState = 10; //判断有多少页，调用接口再动态改
			var souID = []; //获取来源id
			mui.init({
				swipeBack: true, //启用右滑关闭功能
				pullRefresh: { //配置分页下拉刷新
					container: '#pullrefresh',
					down: {
						callback: pulldownRefresh
					},
					up: {
						contentrefresh: '正在加载...',
						callback: pullupRefresh
					}
				}
			});

			//h5监听返回键
			//	plus.key.addEventListener("backbutton",function(){
			//		alert( "BackButton Key pressed!" );
			//	},false);
			//接收采购入库单据页查看来源采购单传来的数据
			if(sessionStorage.getItem('oSouItemId')) {
				var souidData = sessionStorage.getItem('oSouItemId').split(',');
				for(var i = 0; i < souidData.length; i++) {
					souID.push(parseInt(souidData[i]));
				}
			}

			//按时间筛选数据
			mui('body').on('tap', '.pop-tab-con1 li', function() {
				pageIndex = 1;
				startDate = $(this).attr('data-statedate');
				endDate = $(this).attr('data-endDate');
				dataState = true;
				getClientList("pirchaseList", "#listPirchase", "searchContentTips", false);
				mui('#pullrefresh').pullRefresh().refresh(true); //重新启用页面上滑加载
				mui('#pullrefresh').pullRefresh().scrollTo(0, 0, 100); //返回顶部
			});
			//按状态筛选数据
			mui('body').on('tap', '.pop-tab-con2 li', function() {
				pageIndex = 1;
				strStatusId[0] = $(this).attr('data-StatusId');
				dataState = true;
				//		mui('#pullrefresh').pullRefresh().pullupLoading();
				getClientList("pirchaseList", "#listPirchase", "searchContentTips", false);
				mui('#pullrefresh').pullRefresh().refresh(true); //重新启用页面上滑加载
				$('#pullrefresh > .mui-scroll').css('cssText', 'transform: translate3d(0px, 0px, 0px) translateZ(0px); transition-duration: 0ms; transition-timing-function: cubic-bezier(0.075, 0.82, 0.165, 1);');
				//		mui('#pullrefresh').pullRefresh().scrollTo(0,0,100);         //返回顶部
			});

			//单击列表记录传值给详情页
			mui('body').on('tap', '#listPirchase li', function() {
				var purchaseStastusId = $(this).children("a").children("div").children("div").children(".statusId").attr("data-statusid");
				sessionStorage.setItem('PirchaseID', $(this).attr('id'));
				sessionStorage.setItem('purchaseStastusId', purchaseStastusId);
				sessionStorage.removeItem('outPirchaseID');
				sessionStorage.removeItem('strStatusId');
				sessionStorage.removeItem('souceId');
			});

			//初始化页面数据
			getClientList("pirchaseList", "#listPirchase", "initContentTips", true);

		}
	}

	//页面初始化共用事件
	function getClientList(templateId, mainId, contentTips, isChioce) {
		//dataModel需要传的模型值,option非必要参数，默认传空对象，done异步完成，fail异步失败，data.success数值成功返回
		var option = {};
		var dataModel = {
			"id": souID,
			"serial": keyWordText, //(string, optional): 单号 ,
			"companyId": [], //(Array[integer], optional): 客户id eg [1,2,3] ,
			"busUserId": [], //(Array[integer], optional): 业务员 ,
			"statusId": strStatusId, //(Array[integer], optional): 状态值 ,
			"start": startDate, //(string, optional): 开始时间 ,
			"end": endDate, //(string, optional)结束时间 ,
			"orderBy": "", //(string, optional): 排序 ,
			"pageIndex": pageIndex, //(integer, optional),
			"pageSize": 20 //(integer, optional): 分页数；最大分页支持100
		};
		abp.services.app.buyList.getPage(dataModel, option).
		done(function(data) {
			var totalCountNum = data.totalCount; //获取数据列表条数
			pageState = Math.ceil(totalCountNum / 20); //动态修改数据列表页数
			if(totalCountNum < 1) { //动态改变无数据提示
				if(isChioce === true) {
					$('.mui-content > .tipsConInit').html("还没有采购订单记录哦!");
				}
				$('.mui-content').addClass(contentTips);
				$('.mui-pull-top-pocket').css('display', 'none');
			} else {
				$('.mui-content').removeClass(contentTips);
			}
			var items = data.items;
			$.each(items, function(i, v) {
				var serialDate = v.serialDate.substring(0, 10);
				var dataCreated = v.dataCreated.substring(0, 10);
				v.dataCreated = dataCreated;
				v.serialDate = serialDate;
			});

			var html = template(templateId, data);
			if(dataState === true) { //判断是页面刷新还是上滑追加数据
				$(mainId).html(html);
			} else {
				$(mainId).append(html);
			}

			var statusIdColor = require("statusIdColor");
			statusIdColor($(".statusId"));
			sessionStorage.removeItem("oSouItemId");
			if(data.success) {

			} else {
				//	            		console.log(data.error.message)

			}
		}).fail(function(data) {

		});
	}

	//下滑刷新具体业务实现
	function pulldownRefresh() {
		//		startDate='';
		//		endDate='';
		//		strStatusId[0]='';
		pageIndex = 1;
		dataState = true;
		pageState = 10;
		setTimeout(function() {
			getClientList("pirchaseList", "#listPirchase", "searchContentTips", false);
			mui('#pullrefresh').pullRefresh().endPulldownToRefresh(); //刷新完成
			//			mui('#pullrefresh').pullRefresh().endPullupToRefresh(false);    //隐藏已经没数据了
			mui('#pullrefresh').pullRefresh().refresh(true); //重新启用页面上滑加载
		}, 1500);
	}
	//上滑加载具体业务实现
	function pullupRefresh() {
		pageIndex++;
		dataState = false;
		setTimeout(function() {
			mui('#pullrefresh').pullRefresh().endPullupToRefresh((pageState < pageIndex)); //参数为true代表没有更多数据了。
			getClientList("pirchaseList", "#listPirchase", "searchContentTips", false);
		}, 1500);
	}

	//不在钉钉中返回
	mui("body").on("tap", "header>button", function() {
		commonBack.secondBack();
	});

	dingTalkNav.LeftNavBar(commonBack.secondBack);
	//ad
	dingTalkNav.leftAndroid(commonBack.secondBack);
});